package com.zshan.clinic.web.controller.stock;


import com.zshan.clinic.admin.entity.dto.StockDrugProfileDTO;
import com.zshan.clinic.admin.entity.vo.StockDrugProfileDetailVo;
import com.zshan.clinic.admin.entity.vo.StockDrugProfileVo;
import com.zshan.clinic.admin.entity.vo.StockDrugSignatureVo;
import com.zshan.clinic.admin.service.stock.DrugProfileService;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.database.model.StockDrugSignature;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 药品档案
 */
@RestController
@RequestMapping("/drug/profile")
public class DrugProfileController extends AbstractController {


    @Autowired
    private DrugProfileService drugProfileService;

    /**
     * 个人中心：查询药品签名列表
     * @param drugName 药品名称/拼音
     * @param isSignature 是否维护 yesOrNoEnum
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getDrugSignatureList")
    public RestResp<PageInfo<StockDrugSignatureVo>> getDrugSignatureList(@RequestParam(name = "drugName", required = false) String drugName,
                                                                         @RequestParam(name = "isSignature", required = false) String isSignature,
                                                                         @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                         @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<StockDrugSignatureVo> signatureList = drugProfileService.getDrugSignatureList(userInfo.getClinicId(),userInfo.getEmpId(),drugName,isSignature, pageNo, pageSize);
        return RestResp.SUCCESS(signatureList);
    }


    /**
     * 个人中心：修改药品签名
     * @return
     */
    @PostMapping(value = "/updateSignature")
    public RestResp<StockDrugSignatureVo> updateSignature(@RequestBody StockDrugSignature stockDrugSignature) {
        UserInfo userInfo = getUserInfo();
        stockDrugSignature.setEmpId(userInfo.getEmpId());
        stockDrugSignature.setCreateUserId(userInfo.getEmpId());
        stockDrugSignature.setCreateBy(userInfo.getEmpName());
        stockDrugSignature.setUpdateUserId(userInfo.getEmpId());
        stockDrugSignature.setUpdateBy(userInfo.getEmpName());
        stockDrugSignature.setClinicId(userInfo.getClinicId());
        StockDrugSignatureVo nextUnSign = drugProfileService.updateSignature(stockDrugSignature);
        return RestResp.SUCCESS(nextUnSign);
    }

    /**
     * 查询药品列表---模板和门诊选药品用
     * @param storageId 库房ID（模板不用给，门诊需要给）
     * @param drugName 药品名  不能与recoms同时为空
     * @param recoms 模块给推荐场景 门诊给主诉+最终诊断 都是用英文逗号拼接到recoms参数
     * @param drugCategory 药品分类 取值：drugCategoryEnum,如果是成药处方和输液处方给1，如果是中药处方给2
     * @return
     */
    @GetMapping("/getDrugRxList")
    public RestResp<List<StockDrugProfileDetailVo>> getDrugRxList(@RequestParam(name = "storageId", required = false) Long storageId,
                                                                  @RequestParam(name = "drugName",required = false) String drugName,
                                                                  @RequestParam(name = "drugCategory",required = false) String drugCategory,
                                                                  @RequestParam(name = "recoms",required = false) String recoms,
                                                                  @RequestParam(name = "drugIdList",required = false) List<Long> drugIdList) {
        UserInfo userInfo = getUserInfo();
        List<StockDrugProfileDetailVo> drugList = drugProfileService.getDrugRxStorageList(userInfo.getClinicId(),storageId,drugName,recoms,drugCategory,drugIdList);
        return RestResp.SUCCESS(drugList);
    }

    /**
     * 查询药品档案详情
     * @param drugId
     * @return
     */
    @GetMapping("/getDrugProfileDetail")
    public RestResp<StockDrugProfileDetailVo> getDrugProfileDetail(@RequestParam(name = "drugId") Long drugId,
                                                                   @RequestParam(name = "storageId",required = false) Long storageId) {
        UserInfo userInfo = getUserInfo();
        StockDrugProfileDetailVo stockDrugProfileDetailVo = drugProfileService.getDrugProfileDetail(userInfo.getClinicId(), drugId,storageId);
        return RestResp.SUCCESS(stockDrugProfileDetailVo);
    }

    /**
     * 查询药品档案列表
     * @param
     * @return
     */
    @GetMapping("/getDrugProfileList")
    public RestResp<PageInfo<StockDrugProfileVo>> getDrugProfileList(@RequestParam(name = "storageId") Long storageId,
                                                                     @RequestParam(name = "drugName", required = false) String drugName,
                                                                     @RequestParam(name = "drugCategory", required = false) String drugCategory,
                                                                     @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                     @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<StockDrugProfileVo> profileList = drugProfileService.getDrugProfileList(userInfo.getClinicId(), storageId,drugName,drugCategory, pageNo, pageSize);
        return RestResp.SUCCESS(profileList);
    }

    /**
     * 添加药品档案
     * @return
     */
    @PostMapping("/addProfile")
    public RestResp<Void> addProfile(@RequestBody StockDrugProfileDTO profile) {
        UserInfo userInfo = getUserInfo();
        profile.setCreateUserId(userInfo.getEmpId());
        profile.setCreateBy(userInfo.getEmpName());
        profile.setClinicId(userInfo.getClinicId());
        drugProfileService.addProfileDTO(profile);
        return RestResp.SUCCESS();
    }

    /**
     * 编辑药品档案
     * @return
     */
    @PostMapping("/updateProfile")
    public RestResp<Void> updateProfile(@RequestBody StockDrugProfileDTO profile) {
        UserInfo userInfo = getUserInfo();
        profile.setUpdateUserId(userInfo.getEmpId());
        profile.setUpdateBy(userInfo.getEmpName());
        profile.setClinicId(userInfo.getClinicId());
        drugProfileService.updateProfileDTO(profile);
        return RestResp.SUCCESS();
    }

    /**
     * 删除药品档案
     * @return
     */
    @PostMapping("/deleteProfile")
    public RestResp<Void> deleteProfile(@RequestBody StockDrugProfileDTO profile) {
        UserInfo userInfo = getUserInfo();
        profile.setUpdateUserId(userInfo.getEmpId());
        profile.setUpdateBy(userInfo.getEmpName());
        profile.setClinicId(userInfo.getClinicId());
        drugProfileService.deleteProfile(profile);
        return RestResp.SUCCESS();
    }

    /**
     * 启用药品档案
     * @return
     */
    @PostMapping("/enableProfile")
    public RestResp<Void> enableProfile(@RequestBody StockDrugProfileDTO profile) {
        UserInfo userInfo = getUserInfo();
        profile.setUpdateUserId(userInfo.getEmpId());
        profile.setUpdateBy(userInfo.getEmpName());
        profile.setClinicId(userInfo.getClinicId());
        drugProfileService.enableProfile(profile);
        return RestResp.SUCCESS();
    }

    /**
     * 停用药品档案
     * @return
     */
    @PostMapping("/disableProfile")
    public RestResp<Void> disableProfile(@RequestBody StockDrugProfileDTO profile) {
        UserInfo userInfo = getUserInfo();
        profile.setUpdateUserId(userInfo.getEmpId());
        profile.setUpdateBy(userInfo.getEmpName());
        profile.setClinicId(userInfo.getClinicId());
        drugProfileService.disableProfile(profile);
        return RestResp.SUCCESS();
    }
}
